package com.situ.springboot.controller;

import com.situ.springboot.pojo.Admin;
import com.situ.springboot.pojo.query.AdminQuery;
import com.situ.springboot.service.IAdminService;
import com.situ.springboot.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

//@CrossOrigin
@Controller
//@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @RequestMapping("/list")
    @ResponseBody
    public Result list(AdminQuery adminQuery) {
        List<Admin> list = adminService.list(adminQuery);
        //Result result = new Result(0, list);
        return Result.ok(list);
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/toFail")
    public String toFail() {
        return "fail";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(String name, String password, String code, HttpSession session) {
        System.out.println("AdminController.login");
        //首先验证验证码是否正确，再验证用户名密码是否正确
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (code == null || !code.equalsIgnoreCase(codeInSession)) {
            return Result.error("验证码错误");
        }

        Admin admin = adminService.login(name, password);
        if (admin == null) {
            //登录失败，只要告诉前台登录失败这个结果
            //Result result = new Result(1, "登录失败");
            //return result;
            return Result.error("用户名或密码错误");
        } else {
            session.setAttribute("admin", admin);
            //登录成功，告诉前台登录成功
            return Result.ok("登录成功");
        }
    }

    /*@RequestMapping("/login")
    public String login(String name, String password, HttpSession session) {
        System.out.println("AdminController.login");
        Admin admin = adminService.login(name, password);
        if (admin == null) {
            return "redirect:/admin/toFail";
        } else {
            session.setAttribute("admin", admin);
            return "redirect:/";
        }
    }*/

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:/admin/toLogin";
    }

    @RequestMapping("/selectAll")
    public String selectAll(Model model) {
        List<Admin> list = adminService.selectAll();
        model.addAttribute("list", list);
        return "admin_list";
    }

    @RequestMapping("/selectAll2")
    @ResponseBody
    public List<Admin> selectAll2() {
        System.out.println("AdminController.selectAll");
        List<Admin> list = adminService.selectAll();
        return list;

        /*List<Admin> list = new ArrayList<>();
        Admin admin1 = new Admin();
        admin1.setId(1);
        admin1.setName("zhangsan");
        Admin admin2 = new Admin();
        admin2.setId(2);
        admin2.setName("list");
        Admin admin3 = new Admin();
        admin3.setId(3);
        admin3.setName("wangwu");
        list.add(admin1);
        list.add(admin2);
        list.add(admin3);
        return list;*/
    }
}
